package com.future.mayi.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;

import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.adapter.PerformanceAdapter;
import com.future.mayi.app.App;
import com.future.mayi.response.PerformanceResponse;
import com.future.mayi.utils.HCUtils;
import com.future.mayi.view.xrecyclerview.XRecyclerView;

import butterknife.BindView;

public class PerformanceActivity extends BaseTitleActivity implements XRecyclerView.LoadingListener {
    @BindView(R.id.recycler_view)
    XRecyclerView recyclerView;

    private int page;
    private PerformanceAdapter adapter;

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("业绩统计");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_performance);
    }

    @Override
    protected void initView() {
        adapter = new PerformanceAdapter(this, recyclerView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingMoreEnabled(false);
    }

    @Override
    protected void initDates() {
        recyclerView.refresh();
    }

    @Override
    protected void initListener() {
        recyclerView.setLoadingListener(this);
    }

    @Override
    public void onRefresh() {
        page = 0;
        getData();
    }

    @Override
    public void onLoadMore() {
        page++;
        getData();
    }


    private void getData() {
        new HttpRequest<PerformanceResponse>()
                .with(getActivity())
                .setApiCode(ApiCst.quotasStatisticalWallet)
                .addParam("token", App.getToken(mContext))
                .addParam("p", page)
                .addParam("psize", 10)
                .setListener(new HttpRequest.OnNetworkListener<PerformanceResponse>() {

                    @Override
                    public void onSuccess(PerformanceResponse response) {
                        HCUtils.refreshListForPage(recyclerView, adapter, response.list, page, 10);
                    }

                    @Override
                    public void onFail(String message, PerformanceResponse response) {
                        page = HCUtils.refreshFail(recyclerView, page, mContext, message);
                    }
                }).start(new PerformanceResponse());
    }
}
